package gs.wicket.app.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.IClusterable;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RadioChoice;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.apache.wicket.validation.validator.PatternValidator;
import org.apache.wicket.validation.validator.StringValidator;

import gs.wicket.app.Mailer;
import gs.wicket.app.base.BasePage;

public class Kontakt extends BasePage {

	private ContactModel contactModel;
	private Mailer mailer;

	// domain model for Contact
	public class ContactModel implements IClusterable {

		private static final long serialVersionUID = 7833165138299593810L;
		private String vaseIme;
		private String emailAdresa;
		private String naslovPoruke;
		private String poruka;
		private boolean pratiEmailom;
		private String prodavnica;
		
		
		
		public String getVaseIme() {
			return vaseIme;
		}

		public void setVaseIme(String vaseIme) {
			this.vaseIme = vaseIme;
		}

		public String getEmailAdresa() {
			return emailAdresa;
		}

		public void setEmailAdresa(String emailAdresa) {
			this.emailAdresa = emailAdresa;
		}

		public String getNaslovPoruke() {
			return naslovPoruke;
		}

		public void setNaslovPoruke(String naslovPoruke) {
			this.naslovPoruke = naslovPoruke;
		}

		public String getPoruka() {
			return poruka;
		}

		public void setPoruka(String poruka) {
			this.poruka = poruka;
		}

		public boolean isPratiEmailom() {
			return pratiEmailom;
		}

		public void setPratiEmailom(boolean pratiEmailom) {
			this.pratiEmailom = pratiEmailom;
		}

		public String getProdavnica() {
			return prodavnica;
		}

		public void setProdavnica(String prodavnica) {
			this.prodavnica = prodavnica;
		}

		@Override
		public String toString() {

			StringBuilder builder = new StringBuilder();
			builder.append("Naslov: ");
			builder.append(getNaslovPoruke());
			builder.append("\nPoslao: ");
			builder.append(getVaseIme());
			builder.append("\nKontakt: ");
			builder.append(getEmailAdresa());
			builder.append("\nProdavnica: \n");
			builder.append(getProdavnica());
			builder.append("\nSadrzaj: \n");
			builder.append(getPoruka());

			return builder.toString();
		}

		public String toHTMLString() {
			StringBuilder builder = new StringBuilder();
			builder.append("<table border='1'> <tr bgcolor='#FF3300'><td><h3>Naslov </h3></td><td><h3>");
			builder.append(getNaslovPoruke());
			builder.append("</h3></td></tr>");
			builder.append("<tr bgcolor='#FF9900'><td><h5>Poslao </h5></td><td><h5> ");
			builder.append(getVaseIme());
			builder.append("</h5></td></tr><tr bgcolor='##FF9900'><td rowspan='1'bgcolor='#FF9900'><h5>Kontakt </td> <td bgcolor='#FF9900'><h5>");
			builder.append(getEmailAdresa());
			builder.append("</h5></td></tr><tr><td bgcolor='#FFCC00'><h5>Sadržaj:</h5></td><td bgcolor='#FFCC00'>");
			builder.append(getPoruka());
			builder.append("</td></tr></table>");

			return builder.toString();
		}
		
		public ContactModel clear(){
			vaseIme = null;
			emailAdresa=null;
			poruka=null;
			naslovPoruke=null;
			prodavnica=null;
			pratiEmailom=false;
			return this;
		}
	}

	public Kontakt() {
		
		add(new Label("kontakt.message.title", getLangProperty("kontakt.message.title")));
		add(new Label("kontakt.message.intro", getLangProperty("kontakt.message.intro")));
		add(new Label("kontakt.message.warning", getLangProperty("kontakt.message.warning")));

		mailer = new Mailer();

		final Form contactForm = new Form("contactForm");

		RequiredTextField name = new RequiredTextField("vaseIme");
		name.add(StringValidator.lengthBetween(5, 30));

		RequiredTextField emailAddress = new RequiredTextField("emailAdresa");
		emailAddress.add(EmailAddressValidator.getInstance());

		RequiredTextField subject = new RequiredTextField("naslovPoruke");
		subject.add(StringValidator.lengthBetween(5, 30));

		TextArea message = new TextArea("poruka");
		message.add(StringValidator.lengthBetween(10, 200));

		contactModel = new ContactModel();

		contactForm.setModel(new CompoundPropertyModel(
				contactModel));

		CheckBox useEmail = new CheckBox("pratiEmailom");

		List<String> msgShopValue= new ArrayList<String>();
		msgShopValue.add("Sarajevo");
		msgShopValue.add("Tuzla");
		msgShopValue.add("Mostar");
		
		RadioChoice msgShop= new RadioChoice("prodavnica",msgShopValue);
		msgShop.setRequired(true);
		
		contactForm.add(name);;
		contactForm.add(emailAddress);
		contactForm.add(subject);
		contactForm.add(message);
		contactForm.add(useEmail);
		contactForm.add(msgShop);
		contactForm.add(new Label("contact.page.ime.prezime", getLangProperty("contact.page.ime.prezime")));
		contactForm.add(new Label("contact.page.mail", getLangProperty("contact.page.mail")));
		contactForm.add(new Label("contact.page.message.title", getLangProperty("contact.page.message.title")));
		contactForm.add(new Label("contact.page.message.text", getLangProperty("contact.page.message.text")));
		contactForm.add(new Label("contact.page.message.track", getLangProperty("contact.page.message.track")));
		contactForm.add(new Label("contact.page.shop", getLangProperty("contact.page.shop")));
		
		add(contactForm);
		

		// onSubmit implementation using SubmitLink component
		add(new SubmitLink("submitButton", contactForm) {
			private static final long serialVersionUID = 511246638080207216L;
			{
				add(new AttributeModifier("value", new Model(getLangProperty("contact.page.button.submit"))));
			}
			@Override
			public void onSubmit() {
				try {
					mailer.sendEmail(contactModel.getEmailAdresa(), "Shminka ("+contactModel.getProdavnica()+"):"
							+ contactModel.getNaslovPoruke(), contactModel
							.toHTMLString(), contactModel.isPratiEmailom());
					contactForm.setModelObject(contactForm.getModelObject());
					info("Vaša poruka je uspješno poslana.");
				} catch (Exception e) {
					info("Greška prilikom slanja poruke: " + e.getMessage());
				}
			}
		});

	}
}
